/*
 *   Swing Explorer. Tool for developers exploring Java/Swing-based application internals. 
 * 	 Copyright (C) 2012, Maxim Zakharenkov
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *   
 */
package org.swingexplorer;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 *
 * @author Maxim Zakharenkov
 */
public class PNLOptions extends javax.swing.JPanel {
    
    /** Creates new form PNLOptions */
    public PNLOptions() {
        initComponents();
        
        cmbComponentIncludingBorderColor.addItemListener(itemListener);
        cmbComponentWithoutBorderColor.addItemListener(itemListener);
        cmbPreferredSizeColor.addItemListener(itemListener);
        chbDisplayPreferredSize.addItemListener(itemListener);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chbDisplayPreferredSize = new javax.swing.JCheckBox();
        btnReset = new javax.swing.JButton();
        lblComponentWithBorder = new javax.swing.JLabel();
        cmbComponentIncludingBorderColor = new org.swingexplorer.ColorComboBox();
        lblComponentWithoutBorder = new javax.swing.JLabel();
        cmbComponentWithoutBorderColor = new org.swingexplorer.ColorComboBox();
        cmbPreferredSizeColor = new org.swingexplorer.ColorComboBox();
        lblPreferredSize = new javax.swing.JLabel();
        lblBoundaryColors = new javax.swing.JLabel();

        chbDisplayPreferredSize.setText("Display preferred size boundary");
        chbDisplayPreferredSize.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 0, 1, 1));

        btnReset.setText("Reset");
        btnReset.setToolTipText("Reset options to defaults");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        lblComponentWithBorder.setLabelFor(cmbComponentIncludingBorderColor);
        lblComponentWithBorder.setText("Component boundary including border:");

        cmbComponentIncludingBorderColor.setPreferredSize(new java.awt.Dimension(77, 20));

        lblComponentWithoutBorder.setLabelFor(cmbComponentWithoutBorderColor);
        lblComponentWithoutBorder.setText("Component boundary without border:");

        cmbComponentWithoutBorderColor.setPreferredSize(new java.awt.Dimension(77, 20));

        cmbPreferredSizeColor.setPreferredSize(new java.awt.Dimension(77, 20));

        lblPreferredSize.setLabelFor(cmbPreferredSizeColor);
        lblPreferredSize.setText("Component preferred size boundary:");

        lblBoundaryColors.setFont(lblBoundaryColors.getFont().deriveFont(lblBoundaryColors.getFont().getStyle() | java.awt.Font.BOLD));
        lblBoundaryColors.setText("Colors");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblComponentWithBorder)
                            .add(lblComponentWithoutBorder)))
                    .add(lblBoundaryColors)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(lblPreferredSize))
                    .add(chbDisplayPreferredSize))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, cmbPreferredSizeColor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, cmbComponentWithoutBorderColor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(btnReset))
                    .add(cmbComponentIncludingBorderColor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(181, 181, 181))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(3, 3, 3)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(chbDisplayPreferredSize)
                    .add(btnReset))
                .add(3, 3, 3)
                .add(lblBoundaryColors)
                .add(0, 0, 0)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblComponentWithBorder)
                    .add(cmbComponentIncludingBorderColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblPreferredSize)
                            .add(cmbPreferredSizeColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(lblComponentWithoutBorder)
                        .add(cmbComponentWithoutBorderColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(76, 76, 76))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
    	if(options !=null){
    		options.reset();
    		updateFromOptions();
    	}
    }//GEN-LAST:event_btnResetActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JButton btnReset;
    javax.swing.JCheckBox chbDisplayPreferredSize;
    org.swingexplorer.ColorComboBox cmbComponentIncludingBorderColor;
    org.swingexplorer.ColorComboBox cmbComponentWithoutBorderColor;
    org.swingexplorer.ColorComboBox cmbPreferredSizeColor;
    javax.swing.JLabel lblBoundaryColors;
    javax.swing.JLabel lblComponentWithBorder;
    javax.swing.JLabel lblComponentWithoutBorder;
    javax.swing.JLabel lblPreferredSize;
    // End of variables declaration//GEN-END:variables
    
    Options options;
    ItemListenerImpl itemListener = new ItemListenerImpl();
    
    
    public void setOptions(Options _options) {
    	options = _options;
    	updateFromOptions();
    }
    
    private void updateFromOptions() {
    	itemListener.disable();
    	chbDisplayPreferredSize.setSelected(options.isDisplayPreferredSize());
    	cmbComponentIncludingBorderColor.setSelectedItem(options.getComponentIncludingBorderColor());
    	cmbComponentWithoutBorderColor.setSelectedItem(options.getComponentWithoutBorderColor());
    	cmbPreferredSizeColor.setSelectedItem(options.getPreferredSizeColor());
    	itemListener.enable();
    }
    
    private void updateOptions() {
    	options.setDisplayPreferredSize(chbDisplayPreferredSize.isSelected());
    	options.setComponentIncludingBorderColor(cmbComponentIncludingBorderColor.getSelectedColor());
    	options.setComponentWithoutBorderColor(cmbComponentWithoutBorderColor.getSelectedColor());
    	options.setPreferredSizeColor(cmbPreferredSizeColor.getSelectedColor());
	}
    
    class ItemListenerImpl implements ItemListener {
    	
    	private boolean disabled = false;
    	
    	void disable() {
    		disabled = true;
    	}
    	
    	void enable() {
    		disabled = false;
    	}
    	
		public void itemStateChanged(ItemEvent evt) {
			if(disabled) {
				return;
			}
			if(evt.getSource() == chbDisplayPreferredSize || evt.getStateChange() == evt.SELECTED) {
				updateOptions();
			}
		}
    }
    
//    public static void main(String[] args) {
//		JFrame frm = new JFrame();
//		PNLOptions pnlOptions = new PNLOptions();
//		pnlOptions.setOptions(new Options());
//		frm.add(pnlOptions);
//		frm.setBounds(100, 100, 300, 250);
//		frm.setVisible(true);
//	}
}

